/*
 * @Author: chen haoxuan
 * @Date: 2021-06-24 21:39:53
 * @LastEditTime: 2021-06-25 22:28:48
 */
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
  void getData(string filename);
  void addData(string f1, string f2);
  void sortData(string f);
  getData("1.dat");
  getData("2.dat");
  addData("1.dat", "2.dat");
  sortData("2.dat");
  system("pause");
  return 0;
}
void getData(string filename)
{
  ofstream outfile(filename, ios::out);
  if (!outfile)
  {
    cerr << "open " << filename << " error!";
    exit(1);
  }
  int data[10];
  cout << "enter 10 numbers: ";
  for (int i = 0; i < 10; i++)
  {
    cin >> data[i];
    outfile << data[i] << " ";
  }
  outfile.close();
}
void addData(string f1, string f2)
{
  ifstream infile(f1, ios::in);
  if (!infile)
  {
    cerr << "open " << f1 << " error!";
    exit(1);
  }
  ofstream outfile(f2, ios::out | ios::app);
  if (!outfile)
  {
    cerr << "open " << f2 << " error!";
    exit(1);
  }
  char ch;
  while (infile.get(ch))
  {
    outfile.put(ch);
  }
  cout << "add success !" << endl;
  infile.close();
  outfile.close();
}
void sortData(string f)
{
  ifstream infile(f, ios::in);
  if (!infile)
  {
    cerr << "open " << f << " error!";
    exit(1);
  }
  int a[20], i, j;
  for (i = 0; i < 20; i++)
  {
    infile >> a[i];
  }
  int temp;
  for (i = 0; i < 20 - 1; i++)
  {
    for (j = i + 1; j < 20; j++)
    {
      if (a[i] > a[j])
      {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
      }
    }
  }
  ofstream outfile(f, ios::out);
  for (i = 0; i < 20; i++)
  {
    outfile << a[i] << " ";
  }
  cout << "sortData success !" << endl;
  ;
  infile.close();
  outfile.close();
}